package com.zk.cq.send;/**
 * Created by 15911 on 2019/6/30.
 */

import com.zk.cq.config.RobotConfig;
import com.zk.cq.config.RobotFactory;
import com.zk.cq.constant.CoolQAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName Send
 * @Descripyion 调用API
 * @Author zhangkuan
 * @Date 2019/6/30 13:32
 **/
@Slf4j
public class Send {
    private static final String url_prefix = "http://";

    /**
     * 发送私聊
     */
    public static String sendPrivateMsg(Long toQQ, String messgae,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("user_id", toQQ);
        param.put("message", messgae);
        param.put("auto_escape", false);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.send_private_msg));
        log.info("Send>sendPrivate:{}", result);
        return result;
    }

    /**
     * 发送群消息
     *
     * @param group_id
     * @param message
     * @return
     */
    public static String sendGroupMsg(Long group_id, String message,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("message", message);
        param.put("auto_escape", false);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.send_group_msg));
        log.info("Send>sendGroupMsg:{}", result);
        return result;
    }

    /**
     * 发送讨论组消息
     *
     * @param discuss_id
     * @param message
     * @return
     */
    public static String sendDiscussMsg(Long discuss_id, String message,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("discuss_id", discuss_id);//讨论组 ID（正常情况下看不到，需要从讨论组消息上报的数据中获得）
        param.put("message", message);
        param.put("auto_escape", false);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.send_discuss_msg));
        log.info("Send>sendDiscussMsg:{}", result);
        return result;

    }

    /**
     * 通用消息发送
     *
     * @param message_type
     * @param user_id
     * @param group_id
     * @param discuss_id
     * @param message
     * @return
     */
    public static String sendMsg(String message_type, Long user_id, Long group_id, Long discuss_id, String message,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        if (message_type != null) {
            param.put("message_type", message_type);
        }
        if (user_id != null) {
            param.put("user_id", user_id);
        }
        if (group_id != null) {
            param.put("group_id", group_id);
        }
        if (discuss_id != null) {
            param.put("discuss_id", discuss_id);
        }
        param.put("message", message);
        param.put("auto_escape", false);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.send_msg));
        log.info("Send>sendMsg:{}", result);
        return result;

    }

    /**
     * 撤回消息
     *
     * @param message_id
     * @return
     */
    public static String deleteMsg(Long message_id,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("message_id", message_id);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.delete_msg));
        log.info("Send>deleteMsg:{}", result);
        return result;

    }

    /**
     * 发送好友赞
     *
     * @param user_id 对方 QQ 号
     * @param times   赞的次数，每个好友每天最多 10 次
     * @return
     */
    public static String sendLike(Long user_id, Integer times,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("user_id", user_id);
        param.put("times", times);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.send_like));
        log.info("Send>sendLike:{}", result);
        return result;
    }

    /**
     * 群组踢人
     *
     * @param group_id
     * @param user_id            要踢的 QQ 号
     * @param reject_add_request 拒绝此人的加群请求
     * @return
     */
    public static String setGroupKick(Long group_id, Long user_id, Boolean reject_add_request,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("user_id", user_id);
        param.put("reject_add_request", reject_add_request);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_kick));
        log.info("Send>setGroupKick:{}", result);
        return result;
    }

    /**
     * 群组单人禁言
     *
     * @param group_id
     * @param user_id  要禁言的 QQ 号
     * @param duration 禁言时长，单位秒，0 表示取消禁言
     * @return
     */
    public static String setGroupBan(Long group_id, Long user_id, Long duration,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("user_id", user_id);
        param.put("duration", duration);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_ban));
        log.info("Send>setGroupKick:{}", result);
        return result;
    }

    /**
     * 群组匿名用户禁言
     *
     * @param group_id
     * @param anonymous 可选，要禁言的匿名用户对象（群消息上报的 anonymous 字段）
     * @param flag      可选，要禁言的匿名用户的 flag（需从群消息上报的数据中获得）
     * @param duration  禁言时长，单位秒，无法取消匿名用户禁言
     * @return
     */
    public static String setGroupAnonymousBan(Long group_id, Object anonymous, String flag, Long duration,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        if (anonymous != null) {
            param.put("anonymous", anonymous);
        }
        if (flag != null) {
            param.put("flag", flag);
        }
        param.put("duration", duration);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_anonymous_ban));
        log.info("Send>setGroupAnonymousBan:{}", result);
        return result;
    }

    /**
     * 是否禁言
     *
     * @param group_id
     * @param enable
     * @return
     */
    public static String setGroupWholeBan(Long group_id, Boolean enable,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("enable", enable);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_whole_ban));
        log.info("Send>setGroupWholeBan:{}", result);
        return result;
    }

    /**
     * 群组设置管理员
     *
     * @param group_id
     * @param user_id  要设置管理员的 QQ 号
     * @param enable   true 为设置，false 为取消
     * @return
     */
    public static String setGroupAdmin(Long group_id, Long user_id, Boolean enable,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("user_id", user_id);
        param.put("enable", enable);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_admin));
        log.info("Send>setGroupAdmin:{}", result);
        return result;
    }

    /**
     * 群组匿名
     *
     * @param group_id
     * @param enable   是否允许匿名聊天
     * @return
     */
    public static String setGroupAnonymous(Long group_id, Boolean enable,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("enable", enable);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_anonymous));
        log.info("Send>setGroupAnonymous:{}", result);
        return result;
    }

    /**
     * 设置群名片（群备注）
     *
     * @param group_id
     * @param user_id
     * @param card     群名片内容，不填或空字符串表示删除群名片
     * @return
     */
    public static String setGroupCard(Long group_id, Long user_id, String card,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("user_id", user_id);
        param.put("card", card);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_card));
        log.info("Send>setGroupCard:{}", result);
        return result;
    }

    /**
     * 退出群组
     *
     * @param group_id
     * @param is_dismiss 是否解散，如果登录号是群主，则仅在此项为 true 时能够解散
     * @return
     */
    public static String setGroupLeave(Long group_id, Boolean is_dismiss,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("is_dismiss", is_dismiss);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_leave));
        log.info("Send>setGroupLeave:{}", result);
        return result;
    }

    /**
     * 设置群组专属头衔
     *
     * @param group_id
     * @param user_id
     * @param special_title 专属头衔，不填或空字符串表示删除专属头衔
     * @param duration      专属头衔有效期，单位秒，-1 表示永久，不过此项似乎没有效果，可能是只有某些特殊的时间长度有效，有待测试
     * @return
     */
    public static String setGroupSpecialTitle(Long group_id, Long user_id, String special_title, Long duration,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("user_id", user_id);
        param.put("special_title", special_title);
        param.put("duration", duration);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_special_title));
        log.info("Send>setGroupSpecialTitle:{}", result);
        return result;
    }

    /**
     * 退出讨论组
     *
     * @param discuss_id
     * @return
     */
    public static String setDiscussLeave(Long discuss_id,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("discuss_id", discuss_id);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_discuss_leave));
        log.info("Send>setDiscussLeave:{}", result);
        return result;
    }

    /**
     * 处理加好友请求
     *
     * @param flag    加好友请求的 flag（需从上报的数据中获得）
     * @param approve 是否同意请求
     * @param remark  添加后的好友备注（仅在同意时有效）
     * @return
     */
    public static String setFriendAddRequest(String flag, Boolean approve, String remark,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("flag", flag);
        param.put("approve", approve);
        param.put("remark", remark);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_friend_add_request));
        log.info("Send>setFriendAddRequest:{}", result);
        return result;
    }

    /**
     * 处理加群请求／邀请
     *
     * @param flag    加群请求的 flag（需从上报的数据中获得）
     * @param type    add 或 invite，请求类型（需要和上报消息中的 sub_type 字段相符）
     * @param approve 是否同意请求／邀请
     * @param reason  拒绝理由（仅在拒绝时有效）
     * @return
     */
    public static String setGroupAddRequest(String flag, String type, Boolean approve, String reason,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("flag", flag);
        param.put("type", type);
        param.put("approve", approve);
        param.put("reason", reason);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_group_add_request));
        log.info("Send>setGroupAddRequest:{}", result);
        return result;
    }

    /**
     * 获取登录号信息
     *
     * @return
     */
    public static String getLoginInfo(String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.get_login_info));
        log.info("Send>getLoginInfo:{}", result);
        return result;
    }

    /**
     * 获取陌生人信息
     *
     * @param user_id
     * @param no_cache 是否不使用缓存（使用缓存可能更新不及时，但响应更快）
     * @return
     */
    public static String getStrangerInfo(Long user_id, Boolean no_cache,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("flag", user_id);
        param.put("no_cache", no_cache);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.get_stranger_info));
        log.info("Send>getStrangerInfo:{}", result);
        return result;
    }


    /**
     * 获取群列表
     *
     * @return
     */
    public static String getGroupList(String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.get_group_list));
        log.info("Send>getGroupList:{}", result);
        return result;
    }

    /**
     * 获取群成员信息
     *
     * @param group_id
     * @param user_id
     * @param no_cache 是否不使用缓存（使用缓存可能更新不及时，但响应更快）
     * @return
     */
    public static String getGroupMemberInfo(Long group_id, Long user_id, Boolean no_cache,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        param.put("user_id", user_id);
        param.put("no_cache", no_cache);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.get_group_member_info));
        log.info("Send>getGroupMemberInfo:{}", result);
        return result;
    }

    /**
     * 获取群成员列表
     *
     * @param group_id
     * @return
     */
    public static String getGroupMemberList(Long group_id,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.get_group_member_list));
        log.info("Send>getGroupMemberList:{}", result);
        return result;
    }


    /**
     * 获取插件运行状态
     *
     * @return
     */
    public static String getStatus(String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.get_status));
        log.info("Send>getStatus:{}", result);
        return result;
    }

    /**
     * 获取 酷Q 及 HTTP API 插件的版本信息
     *
     * @return
     */
    public static String getVersionInfo(String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.get_version_info));
        log.info("Send>getVersionInfo:{}", result);
        return result;
    }

    /**
     * 重启 HTTP API 插件
     *
     * @param delay 要延迟的毫秒数，如果默认情况下无法重启，可以尝试设置延迟为 2000 左右
     * @return
     */
    public static String setRestartPlugin(Integer delay,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("delay", delay);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI.set_restart_plugin));
        log.info("Send>setRestartPlugin:{}", result);
        return result;
    }

    /**
     * 获取好友列表
     *
     * @param flat 是否获取扁平化的好友数据，即所有好友放在一起、所有分组放在一起，而不是按分组层级
     * @return
     */
    public static String _getFriendList(Boolean flat,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("flat", flat);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI._get_friend_list));
        log.info("Send>_getFriendList:{}", result);
        return result;
    }

    /**
     * 获取群信息
     *
     * @param group_id
     * @return
     */
    public static String _getGroupInfo(Long group_id,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI._get_group_info));
        log.info("Send>_getGroupInfo:{}", result);
        return result;
    }

    /**
     * 获取群公告
     *
     * @param group_id
     * @return
     */
    public static String _getGroupNotice(Long group_id,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI._get_group_notice));
        log.info("Send>_getGroupNotice:{}", result);
        return result;
    }

    /** 发布群公告
     * @param group_id
     * @param title
     * @param content
     * @return
     */
    public static String _sendGroupNotice(Long group_id, String title, String content,String robotAddr) {
        Map<String, Object> param = new HashMap<>();
        param.put("group_id", group_id);
        String result = post(param, url_prefix.concat(robotAddr).concat(CoolQAPI._send_group_notice));
        log.info("Send>_sendGroupNotice:{}", result);
        return result;
    }


    private static String post(Map<String, Object> param, String url) {
        RestTemplate restTemplate = RobotFactory.getRestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<Map<String, Object>> request = new HttpEntity<>(param, headers);
        return restTemplate.postForObject(url, request, String.class);

    }

}
